package jiguang.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.event.HomepageMsgEvent;
import com.jiuyin.dianjing.ui.fragment.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_ID;

/**
 * Description:
 * File: HomePageFragment.java
 * Author: Administrator
 * Create: 2020/7/6 17:12
 */
public class HomePageFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_id)
    TextView mTvID;
    @BindView(R.id.tv_sign)
    TextView mTvSign;
    @BindView(R.id.tv_attention)
    TextView mTvAttention;
    @BindView(R.id.tv_subscription)
    TextView mTvSubscription;
    @BindView(R.id.tv_fans)
    TextView mTvFans;
    private String mId;

    public static HomePageFragment newInstance(String id) {
        HomePageFragment fragment = new HomePageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void fetchData() {
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_home_page;
    }

    @Override
    protected void initData() {
        Bundle argumentsBundle = getArguments();
        if (null != argumentsBundle) {
            mId = argumentsBundle.getString(KEY_ID);
        }
        HomePageInfoFragment homepage = HomePageInfoFragment.newInstance(mId);
        getChildFragmentManager().beginTransaction()
                .replace(R.id.layout_fragment, homepage)
                .commit();
    }

    @Override
    protected void initView() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HomepageMsgEvent event) {
        refresh(event);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 更新数据
     *
     * @param event
     */
    private void refresh(HomepageMsgEvent event) {
        mTvAttention.setText(getString(R.string.attention, event.attention_num));
        mTvFans.setText(getString(R.string.fans, event.fans_num));
        mTvSubscription.setText(getString(R.string.subscribe, event.topic_num));
        mTvName.setText(event.nickname);
        mTvID.setText(getString(R.string.id, event.id));
        mTvSign.setText(event.sign);
    }
}
